package itesm.coler.data;

import itesm.coler.data.DAOUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

import javax.sql.*;

import itesm.coler.models.MessageCategory; 

public class MySQLMessageCategoryDAO implements MessageCategoryDAO{

	Connection connection = null;
	
	public MySQLMessageCategoryDAO()
	{
		
	}
	
	public Connection getConnection(){
		if (connection == null)
		    connection = MySQLDAOFactory.createConnection();
		return connection;
	}
	
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	
	@Override
	public MessageCategory findMessageCategoryByMessageCategoryId(
			String messageCategoryId) throws DAOException {
		// Implement find a MessageCategory here using supplied
				 // argument values as search criteria
				 // Return a Transfer Object if found,
				 // return null on error or if not found
		        PreparedStatement findMessageCategory = null;
		        ResultSet results= null;
		        MessageCategory messageCategory = null;
				try{
						  connection = getConnection();
						  if (connection!=null){
							  findMessageCategory = connection.prepareStatement("select * from messagecategory where messagecategoryid = ?");
							  findMessageCategory.setString(1,messageCategoryId);
							  results = findMessageCategory.executeQuery();
							  if (results.next()) {
								  messageCategory = mapResultSetToObject(results);						  			  	
							  }
						  }
				}catch(SQLException e){
					throw new DAOException(e);
				} finally {
					DAOUtil.close(connection,findMessageCategory,results);
				}
				return messageCategory;
	}

	@Override
	public MessageCategory findMessageCategoryByMessageCategoryName(
			String messageCategoryName) throws DAOException {
		 // Implement find a MessageCategory here using supplied
		 // argument values as search criteria
		 // Return a Transfer Object if found,
		 // return null on error or if not found
       PreparedStatement find = null;
       ResultSet results= null;
       MessageCategory messageCategory = null;
		try{
			      connection = getConnection();
				  if (connection!=null){
					  find = connection.prepareStatement("select * from messagecategory where messagecategoryname = ?");
					  find.setString(1,messageCategoryName);
					  results = find.executeQuery();
					  if (results.next()) {
						  messageCategory = mapResultSetToObject(results);						  			  	
					  }
				  }
		}catch(SQLException e){
			throw new DAOException(e);
		} finally {
			DAOUtil.close(connection,find,results);
		}
		return messageCategory;
	}

	@Override
	public ArrayList<MessageCategory> findAll() throws DAOException {
		 // implement search MessageCategorys here using the
	    // supplied criteria.
	    // Alternatively, implement to return a Collection 
	    // of Transfer Objects.
		System.out.println("Voy a hacer consulta en findall");
         PreparedStatement findMessageCategory = null;
         ResultSet results= null;
		 ArrayList<MessageCategory> messageCategoryList = null;
		 try {
		       connection = getConnection();
		       findMessageCategory = connection.prepareStatement("SELECT * FROM messagecategory");
		       synchronized(findMessageCategory) {
		       // obtain list of MessageCategorys	  
		    	  results = findMessageCategory.executeQuery();
		       // get rows data{
		    	  messageCategoryList = new ArrayList<MessageCategory>();
		         while (results.next()) {
		        	 MessageCategory messageCategory = mapResultSetToObject(results);		
		        	 messageCategoryList.add(messageCategory);	   
		         }
		       }
		   } catch (SQLException e){
			   throw new DAOException(e);
		   } finally {
				DAOUtil.close(connection,findMessageCategory,results);
		   }
		  return messageCategoryList;
	}

	
	public MessageCategory mapResultSetToObject(ResultSet results) throws SQLException {
		  System.out.println("Voy a mapear el objeto MessageCategory");
		  MessageCategory messageCategory = new MessageCategory();
		  messageCategory.setMessageCategoryId(results.getString("messagecategoryid"));
		  messageCategory.setMessageCategoryName(results.getString("messagecategoryname"));
		  messageCategory.setPreferenceLevel(results.getInt("preferencelevel"));
		  messageCategory.setActive(results.getBoolean("active"));
		 		
		  return messageCategory;
	 }

	@Override
	public RowSet selectMessageCategoriesRS() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<MessageCategory> selectMessageCategoriesTO() {
		// TODO Auto-generated method stub
		return null;
	}

}
